package cn.group.hotel2.service.impl;

import cn.group.hotel2.ex.ServiceException;
import cn.group.hotel2.mapper.OrderMapper;
import cn.group.hotel2.mapper.RoomCheckOutMapper;
import cn.group.hotel2.pojo.dto.RoomCheckOutAddNewDTO;
import cn.group.hotel2.pojo.dto.RoomCheckOutUpdateDTO;
import cn.group.hotel2.pojo.entity.RoomCheckOut;
import cn.group.hotel2.pojo.vo.OrderStandardVO;
import cn.group.hotel2.pojo.vo.RoomCheckOutListCountVO;
import cn.group.hotel2.pojo.vo.RoomCheckOutListItemVO;
import cn.group.hotel2.pojo.vo.RoomCheckOutStandardVO;
import cn.group.hotel2.service.IRoomCheckOutService;
import cn.group.hotel2.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

/**
 * 处理退房登记数据相关的业务实现类
 * 
 * @author wcl
 * @version 0.0.1
 * @date 2022/11/25
 */
@Slf4j
@Service
public class RoomCheckOutServiceImpl implements IRoomCheckOutService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RoomCheckOutMapper roomCheckOutMapper;

    public RoomCheckOutServiceImpl() {
        log.debug("创建业务类对象:RoomCheckOutServiceImpl");
    }

    @Override
    public void addNew(RoomCheckOutAddNewDTO roomCheckOutAddNewDTO) {
        log.debug("开始处理【新增退房登记】的业务,参数:{}", roomCheckOutAddNewDTO);

        String orderNum = roomCheckOutAddNewDTO.getOrderNum();
        OrderStandardVO queryResult = orderMapper.getStandardByOrderNum(orderNum);
        log.debug("根据订单编号【{}】获取订单详情,结果为:{}", orderNum, queryResult);
        if(queryResult == null) {
            String message = "新增退房登记失败,该订单编号不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_ORDER_NOT_FOUND, message);
        }
        log.debug("订单编号存在!");


        Long orderId = queryResult.getId();
        LocalDateTime checkOutTime = LocalDateTime.now();
        Integer returnPay = roomCheckOutAddNewDTO.getReturnPay();
        RoomCheckOut roomCheckOut = new RoomCheckOut();
        roomCheckOut.setOrderId(orderId);
        roomCheckOut.setReturnPay(returnPay);
        roomCheckOut.setCheckOutTime(checkOutTime);
        log.debug("即将向退房登记表中插入数据,结果为:{}", roomCheckOut);
        int rows = roomCheckOutMapper.insert(roomCheckOut);
        if(rows != 1) {
            String message = "新增退房登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("新增完成!");
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据记录id删除退房登记单条数据】的业务,参数:{}", id);

        RoomCheckOutStandardVO queryResult = roomCheckOutMapper.getStandardById(id);
        log.debug("根据id【{}】查询退房登记详情,结果为:{}", id, queryResult);
        if (queryResult == null) {
            String message = "删除退房登记失败,该条数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("即将删除该条退房登记");
        int rows = roomCheckOutMapper.deleteById(id);
        if(rows != 1) {
            String message = "删除退房登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("删除完成!");
    }

    @Override
    public void updateById(Long id, RoomCheckOutUpdateDTO roomCheckOutUpdateDTO) {
        log.debug("开始处理【根据订单id修改退房登记信息】的业务,参数:{},{}", id, roomCheckOutUpdateDTO);

        String updateOrderNum = roomCheckOutUpdateDTO.getOrderNum();
        List<RoomCheckOutListItemVO> list = roomCheckOutMapper.list();
        Iterator<RoomCheckOutListItemVO> iterator = list.iterator();
        while (iterator.hasNext()) {
            RoomCheckOutListItemVO item = iterator.next();
            if(item.getOrderNum().equals(updateOrderNum)) {
                iterator.remove();
                break;
            }
        }
        log.debug("移除自身数据后的退房登记列表:{}", list);
        for (RoomCheckOutListItemVO roomCheckOutListItemVO : list) {
            String orderNumListItemVO = roomCheckOutListItemVO.getOrderNum();
            if(orderNumListItemVO.equals(updateOrderNum)){
                String message = "修改退房登记详情失败,该订单号已存在！";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        OrderStandardVO queryResult = orderMapper.getStandardByOrderNum(updateOrderNum);
        if (queryResult == null) {
            String message = "修改退房登记失败,该条退房登记信息不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_ORDER_NOT_FOUND, message);
        }

        Long orderId = queryResult.getId();
        RoomCheckOut roomCheckOut = new RoomCheckOut();
        Integer returnPay = roomCheckOutUpdateDTO.getReturnPay();
        roomCheckOut.setId(id);
        roomCheckOut.setOrderId(orderId);
        roomCheckOut.setReturnPay(returnPay);
        log.debug("即将修改退房登记信息,结果为:{}", roomCheckOut);
        int rows = roomCheckOutMapper.update(roomCheckOut);
        if(rows != 1) {
            String message = "修改退房登记失败,系统繁忙,请稍后再次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
        log.debug("修改退房登记信息完成!");
    }

    @Override
    public RoomCheckOutStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据退房登记id查询登记数据】的业务,参数:{}", id);

        RoomCheckOutStandardVO queryResult = roomCheckOutMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询登记数据失败,该条数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        log.debug("查询完成,结果为:{}",queryResult);
        return queryResult;
    }

    @Override
    public List<RoomCheckOutListItemVO> list() {
        log.debug("开始处理【查询退房登记列表】的业务,无参数");
        return roomCheckOutMapper.list();
    }

    @Override
    public List<RoomCheckOutListItemVO> listByPageNum(Long pageNum) {
        log.debug("开始处理【根据所选的页数来分页查询】的业务,参数:{}", pageNum);
        return roomCheckOutMapper.listByPageNum(pageNum);
    }

    @Override
    public List<RoomCheckOutListCountVO> listCount() {
        return roomCheckOutMapper.listCount();
    }
}
